/*
 * QueryGenerator.java
 *
 * Created on May 10, 2007, 12:25 AM
 *
 *  Life Logger v1.0
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.karma.search.query;

import com.semanticvoid.lifelogger.configuration.Constants;

/**
 *
 * @author anand kishore
 */
public class QueryGenerator {
    
    public static String generateSearchQueryForBrowseHistory(String queryTerms, short type, String fromDate, String toDate) {
//        if(queryTerms == null || queryTerms.equals("")) {
//            return null;
//        }
        
        StringBuffer query = new StringBuffer();
        
        if(queryTerms == null || queryTerms.equals("null")) {
            // If the type = ALL then generate a query string without the type filter
            if(type == Constants.ITEM_TYPE_FOR_ALL) {
                query.append("select *, 0 score from browse_history");
                
                if(fromDate != null && toDate != null) {
                    query.append(" where " + formDateRangeSubQuery(fromDate, toDate));
                }
            } else {
                query.append("select *, 0 score from browse_history where type = " + type);
                
                if(fromDate != null && toDate != null) {
                    query.append(" and " + formDateRangeSubQuery(fromDate, toDate));
                }
            }
        } else {
            // If the type = ALL then generate a query string without the type filter
            if(type == Constants.ITEM_TYPE_FOR_ALL) {
                query.append("select *, match(title, content) against('" + queryTerms + "') score from browse_history where guid in (select guid from browse_history where match(title, content) against('" + formBooleanQuery(queryTerms) + "' in boolean mode)) and match(title, content) against('" + queryTerms + "')");
                
                if(fromDate != null && toDate != null && !fromDate.equals("") && !toDate.equals("")) {
                    query.append(" and " + formDateRangeSubQuery(fromDate, toDate));
                }
                
                query.append(" group by link order by score desc");
            } else {
                query.append("select *, match(title, content) against('" + queryTerms + "') score from browse_history where guid in (select guid from browse_history where type = " + type + " and match(title, content) against('" + formBooleanQuery(queryTerms) + "' in boolean mode)) and match(title, content) against('" + queryTerms + "')");
                
                if(fromDate != null && toDate != null && !fromDate.equals("") && !toDate.equals("")) {
                    query.append(" and " + formDateRangeSubQuery(fromDate, toDate));
                }
                
                query.append(" group by link order by score desc");
            }
        }
        
        return query.toString();
    }
    
    public static String generateSearchQueryForSearchHistory(String queryTerms, String fromDate, String toDate) {
//        if(queryTerms == null || queryTerms.equals("")) {
//            return null;
//        }
        
        StringBuffer query = new StringBuffer();
        
        if(queryTerms == null || queryTerms.equals("null")) {
            query.append("select *, 0 score from search_history");
            
            if(fromDate != null && toDate != null) {
                query.append(" where " + formDateRangeSubQuery(fromDate, toDate));
            }
        } else {
            query.append("select *, match(keywords) against('" + queryTerms + "') score from search_history where guid in (select guid from search_history where match(keywords) against('" + formBooleanQuery(queryTerms) + "' in boolean mode)) and match(keywords) against('" + queryTerms + "')");
        
            if(fromDate != null && toDate != null && !fromDate.equals("") && !toDate.equals("")) {
                    query.append(" and " + formDateRangeSubQuery(fromDate, toDate));
                }
                
            query.append(" group by keywords order by score desc");
        }
        
        return query.toString();
    }
    
    private static String formDateRangeSubQuery(String fromDate, String toDate) {
        if(fromDate == null || toDate == null) {
            return "";
        }
        
        String subQuery = " (pubDate >= \"" + fromDate + " 00:00:00\" and pubDate <= \"" + toDate + " 23:59:59\") ";
        
        return subQuery;
    }
    
    private static String formBooleanQuery(String queryTerms) {
        String[] tokens = queryTerms.split(" ");
        StringBuffer boolQuery = new StringBuffer();
        
        for(String token : tokens) {
            boolQuery.append("+" + token + " ");
        }
        
        return boolQuery.toString();
    }
}
